<template>
  <div>
    <!-- 菜单信息 用于新增或者修改  应该是一个表单 -->
    <el-dialog
      :title="info.type === 'add' ? '新增' : '修改'"
      :visible.sync="info.isShow"
      width="60%"
    >
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-tabs v-model="activeName" @tab-click="handleClick">
          <el-tab-pane label="基础数据" name="first">
            <el-form-item label="一级分类" prop="first_cateid">
              <el-select
                v-model="ruleForm.first_cateid"
                placeholder="请选择一级分类"
                @change="getFirst"
              >
                <el-option
                  v-for="(item, index) in firstOptionList"
                  :key="index"
                  :label="item.catename"
                  :value="item.id"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="二级分类" prop="second_cateid">
              <el-select
                v-model="ruleForm.second_cateid"
                placeholder="请选择一级分类"
              >
                <el-option
                  v-for="(menu, index) in secondOptionList"
                  :key="index"
                  :label="menu.catename"
                  :value="menu.id"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="名称" prop="goodsname">
              <el-input v-model="ruleForm.goodsname"></el-input>
            </el-form-item>
            <el-form-item label="商品价格" prop="price">
              <el-input v-model="ruleForm.price"></el-input>
            </el-form-item>
            <el-form-item label="市场价格" prop="market_price">
              <el-input v-model="ruleForm.market_price"></el-input>
            </el-form-item>
            <el-form-item label="图片" prop="img"> </el-form-item>
            <el-form-item label="商品规格" prop="sepcsid">
              <el-select
                v-model="ruleForm.sepcsid"
                placeholder="请选择商品规格"
              >
                <el-option
                  v-for="(menu, index) in secondOptionList"
                  :key="index"
                  :label="menu.rolename"
                  :value="menu.id"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="规格属性值" prop="sepcsattr">
              <el-select
                v-model="ruleForm.sepcsattr"
                multiple
                placeholder="请选择规格属性值"
              >
                <el-option
                  v-for="(menu, index) in secondOptionList"
                  :key="index"
                  :label="menu.rolename"
                  :value="menu.id"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="是否新品">
              <el-switch
                v-model="ruleForm.isnew"
                active-color="#13ce66"
                inactive-color="#ff4949"
                :active-value="1"
                :inactive-value="2"
              >
              </el-switch>
            </el-form-item>
            <el-form-item label="是否热卖">
              <el-switch
                v-model="ruleForm.ishot"
                active-color="#13ce66"
                inactive-color="#ff4949"
                :active-value="1"
                :inactive-value="2"
              >
              </el-switch>
            </el-form-item>
            <el-form-item label="状态">
              <el-radio-group v-model="ruleForm.status">
                <el-radio :label="1">正常</el-radio>
                <el-radio :label="2">禁用</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-tab-pane>
          <el-tab-pane label="商品描述" name="second">
            <div id="editor"></div>
          </el-tab-pane>
        </el-tabs>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="close()">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import router from "@/router";
import E from "wangeditor";
import { getCateListTree, getCateListByPid } from "../../apis";
export default {
  props: ["info"],
  data() {
    return {
      // 选项卡的激活项
      activeName: "first",
      editor:null,
      // 表单的数据
      ruleForm: {
        first_cateid: 1,
        second_cateid: "",
        password: "",
        goodsname: "",
        price: "",
        market_price: "",
        img: "",
        description: "",
        sepcsid: "",
        sepcsattr: "",
        isnew: 1,
        ishot: 1,
        status: 1,
      },
      rules: {
        // 只有用户名是必须添的
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
        ],
      },
      // 下拉列表的数据
      firstOptionList: [],
      secondOptionList: [],
    };
  },
  created() {
    // 获取下框对应的数据
    this.getOptionList();
  },
  methods: {
    handleClick() {
      if (this.activeName && !this.editor) {
        // 切换到商品详情这个模块时 且编辑器不存在 再创建编辑器
        const editor = new E("#editor");
        editor.create();
        this.editor = editor // 避免重复创建
      }
      // 修改商品时 显示之前的商品描述
      if(this.editor){
        // 获取商品描述的内容 显示在页面上 需要用txt.html() 这个方法
      this.editor.txt.html(this.ruleForm.description);
      }
    },
    // 这个函数的作用就是给ruleForm赋值，以显示表单的内容
    setVal(val) {
      this.ruleForm.description = '';
      this.ruleForm = { ...val };
      
    },
    async getOptionList() {
      let res = await getCateListTree();
      this.firstOptionList = res.list;
    },
    // 一级分类的变化
    async getFirst() {
      // 每当一级分类变化时 都要获取对应的二级分类的数组 放在二级分类的下拉框中
      let res = await getCateListByPid(this.ruleForm.first_cateid);
      this.secondOptionList = res.list;
    },
    submit() {
      this.$refs.ruleForm.validate(async (valid) => {
        if (valid) {
          this.ruleForm.description = this.editor.txt.html();
          return
          let res;
          // 区分新增还是修改
          if (this.info.type === "add") {
            // 新增
            res = await addUser(this.ruleForm);
          } else {
            // 修改
            res = await editMenu(this.ruleForm);
          }
          if (res.code == 200) {
            // 更新成功，通知list组件重新获取数据来更新列表
            this.$emit("update");
          }
          // 关闭弹窗
          this.close();
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    close() {
      this.info.isShow = false;
    },
  },
};
</script>

<style>
#editor {
  width: 90%;
}
</style>
